package com.example.hszpad.util;

import android.util.Xml;

import com.example.hszpad.bean.ConfigBean;
import com.example.hszpad.bean.DataPointBean;
import com.example.hszpad.bean.TolietBean;
import com.example.hszpad.bean.VideoBean;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * xml解析工具
 * Created by Administrator on 2017/4/21.
 */

public class XmlParserUtil {
    public static ConfigBean parse(String filePath) {
        XmlPullParser parser = null;
        ConfigBean bean = null;
        Map<String, DataPointBean> dataPointBeanMap = null;
        List<DataPointBean> showList = new ArrayList<>();
        DataPointBean dataPointBean = null;
        try {
            parser = Xml.newPullParser();
            parser.setInput(new FileInputStream(filePath), "utf-8");
            int eventType = parser.getEventType();
            String name = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        name = parser.getName();
                        if ("config".equals(name)) {
                            bean = new ConfigBean();
                        } else if ("controlIP".equals(name)) {
                            bean.setControlIP(parser.nextText());
                        } else if ("controlPort".equals(name)) {
                            bean.setControlPort(Integer.parseInt(parser.nextText()));
                        } else if ("ip".equals(name)) {
                            bean.setIp(parser.nextText());
                        } else if ("ip".equals(name)) {
                            bean.setIp(parser.nextText());
                        } else if ("port".equals(name)) {
                            bean.setPort(Integer.parseInt(parser.nextText()));
                        } else if ("datapointlist".equals(name)) {
                            dataPointBeanMap = new HashMap<String, DataPointBean>();
                        } else if ("datapointbean".equals(name)) {
                            dataPointBean = new DataPointBean();
                        } else if ("id".equals(name)) {
                            dataPointBean.setId(Integer.parseInt(parser.nextText()));
                        } else if ("address".equals(name)) {
                            dataPointBean.setAddress(Integer.parseInt(parser.nextText()));
                        } else if ("width".equals(name)) {
                            dataPointBean.setWidth(Integer.parseInt(parser.nextText()));
                        } else if ("height".equals(name)) {
                            dataPointBean.setHeight((Integer.parseInt(parser.nextText())));
                        } else if ("x".equals(name)) {
                            dataPointBean.setX((Integer.parseInt(parser.nextText())));
                        } else if ("y".equals(name)) {
                            dataPointBean.setY((Integer.parseInt(parser.nextText())));
                        } else if ("offset".equals(name)) {
                            dataPointBean.setOffset((Integer.parseInt(parser.nextText())));
                        } else if ("type".equals(name)) {
                            dataPointBean.setType((Integer.parseInt(parser.nextText())));
                        } else if ("onImage".equals(name)) {
                            dataPointBean.setOnImage(parser.nextText());
                        } else if ("offImage".equals(name)) {
                            dataPointBean.setOffImage(parser.nextText());
                        } else if ("isLight".equals(name)) {
                            dataPointBean.setIsLight(Integer.parseInt(parser.nextText()));
                        } else if ("dataType".equals(name)) {
                            dataPointBean.setDataType(parser.nextText().toUpperCase());
                        } else if ("bitIndex".equals(name)) {
                            dataPointBean.setBitIndex(Integer.parseInt(parser.nextText()));
                        } else if ("condition".equals(name)) {
                            dataPointBean.setCondition(parser.nextText());
                        } else if ("onShow".equals(name)) {
                            dataPointBean.setOnShow(Integer.parseInt(parser.nextText()));
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        name = parser.getName();
                        if ("datapointbean".equals(name)) {
                            dataPointBean.setVal("0.00");
                            if (dataPointBean.getOnShow() == 1) {
                                showList.add(dataPointBean);
                            } else {
                                dataPointBeanMap.put(String.valueOf(dataPointBean.getId()), dataPointBean);
                            }
                        } else if ("datapointlist".equals(name)) {
                            bean.setDataPointBeanMap(dataPointBeanMap);
                        } else if ("config".equals(name)) {
                            bean.setDataPointBeanOnShowList(showList);
                            return bean;
                        }
                        break;
                }
                eventType = parser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
            bean = null;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            bean = null;
        } catch (IOException e) {
            e.printStackTrace();
            bean = null;
        }
        return bean;
    }

}
